package com.qianfeng.smsplatform.monitor.config;

//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼                  BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//  


import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.qianfeng.smsplatform.monitor.tasks.QueueMesageTask;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by jackiechan on 2021/8/6/20:34
 * @Author jackiechan
 */
@Configuration
public class ElasticJobConfig {

    private String serverList="192.168.3.91:2181";

    private String namespace="online07";
    private String corn="0/30 * * * * ?";

    @Bean(initMethod = "init")
    public CoordinatorRegistryCenter coordinatorRegistryCenter() {
        ZookeeperConfiguration configuration = new ZookeeperConfiguration(serverList, namespace);
        configuration.setConnectionTimeoutMilliseconds(10000);
        configuration.setSessionTimeoutMilliseconds(100000);
        return new ZookeeperRegistryCenter(configuration);
    }

    @Bean
    public LiteJobConfiguration liteJobConfiguration() {
        //数字5 指的是我们当前的任务分成几份来执行,目的是可以分发到不同的机器上面,让每个机器都参与任务,每个机器都执行一部分,这样可以合理利用资源
        //         JobCoreConfiguration jobCoreConfiguration = JobCoreConfiguration.newBuilder("suibianxiegemingzi,",corn,5).shardingItemParameters("0=liling,1=wangyi,2=zhaoer,3=zhangsan,4=lisi").build();
        JobCoreConfiguration jobCoreConfiguration = JobCoreConfiguration.newBuilder("suibianxiegemingzi,",corn,1).shardingItemParameters(null).build();
        SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(jobCoreConfiguration, QueueMesageTask.class.getCanonicalName());
        LiteJobConfiguration jobConfiguration = LiteJobConfiguration.newBuilder(simpleJobConfiguration).build();
        return jobConfiguration;
    }

    @Bean(initMethod = "init")
    public SpringJobScheduler springJobScheduler(QueueMesageTask task,CoordinatorRegistryCenter coordinatorRegistryCenter,LiteJobConfiguration liteJobConfiguration) {
        return new SpringJobScheduler(task,coordinatorRegistryCenter,liteJobConfiguration);
    }
}
